roles = function() {
	//定义添加修改角色窗体
	var ROLESADDOREDITWIN = null;
	//定义搜索角色窗体
	var ROLESSEARCHWIN = null;
	//定义权限设置窗体
	var ROLESCONFIGWIN = null;
	return {
		/*--------------------------------------窗体定义--------------------------------------------*/
		//定义角色GRID窗体
		panel : function() {
			var store = new Store({
				fields: ['id', 'name', 'remark'],
				proxy: {
			        type: 'ajax',
			        url: '../admin/role.do?index',
			        reader: {
			            type: 'json',
			            root: 'datas',
			            totalProperty: 'total'
			        }
			    }
			});
			
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
					name: ''
			};
			store.load();
			
			var pageToolBar = new PagingToolbar({
	            store: store,
	            draggable : false,
	            displayInfo: true
	        });
			
			var grid = new Grid({
				title: '角色列表',
				id: 'rolegrid',
				region: 'center',
				store: store,
				minColumnWidth: 35,
				stripeRows: true,
				bbar : pageToolBar,
				tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						store.load();
					}
				}, "-", {
					text: '搜索',
					scale: 'medium',
					iconCls: 'icon-search',
					handler: function() {
						roles.searchRoleAction();
					}
				}, "-", {
					id: 'roleAdd',
					text: '添加',
					scale: 'medium',
					iconCls: 'icon-add',
					disabled: true,
					handler: function() {
						roles.addRoleAction();
					}
				}, "-", {
					id: 'roleEdit',
					text: '修改',
					scale: 'medium',
					iconCls: 'icon-edit',
					disabled: true,
					handler: function() {
						roles.editRoleAction();
					}
				}, "-", {
					id: 'roleRemove',
					text: '删除',
					scale: 'medium',
					iconCls: 'icon-remove',
					disabled: true,
					handler: function() {
						roles.removeAction();
					}
				}, "-", {
					id: 'roleConfig',
					text: '权限设置',
					scale: 'medium',
					iconCls: 'icon-config',
					disabled: true,
					handler: function() {
						roles.configAction();
					}
				}],
				columns: [
		             new Ext.grid.RowNumberer(),
		             {header: '序号', dataIndex: 'id', hidden: true},
		             {header: '角色名称', dataIndex: 'name', width: 300},
		             {header: '备注说明', dataIndex: 'remark', flex: 1}
				],
				listeners: {
					select: function(fn, rec, index, eOpts) {
					}
				}
			});
			
			var panel = new Panel({
				border: false,
				layout: 'border',
				items: [grid]
			});
			
			return panel;
		},
		
		//搜索角色
		searchWin: function() {
			if (ROLESSEARCHWIN == null) {
				ROLESSEARCHWIN = new Window({
					id: 'roleSearchWin',
				    title: '搜索角色',
				    height: 150,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items:[{
				        	fieldLabel: '角色名称',
				        	name: 'name'
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		roles.searchSureAction(fom);
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		ROLESSEARCHWIN.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			ROLESSEARCHWIN.show();
		},
		
		//添加修改角色
		addOrEditWin: function() {
			if (ROLESADDOREDITWIN == null) {
				ROLESADDOREDITWIN = new Window({
					id: 'roleAddOrEditWin',
				    title: '角色信息编辑',
				    height: 200,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	id: 'roleAddOrEditForm',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'id',
				        	hidden: true
				        }, {
				        	fieldLabel: '角色名称',
				        	name: 'name',
				        	allowBlank: false
				        }, {
				        	xtype: 'textareafield',
				        	fieldLabel: '备注说明',
				        	name: 'remark'
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		if (fom.isValid()) {
				        			roles.sumbitAction(fom);
				        		}
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		ROLESADDOREDITWIN.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			ROLESADDOREDITWIN.show();
		},
		
		//权限设置
		configWin: function(id) {
			//加载角色权限
			var res = RequestByUrl('../admin/role.do?configtoaction', {id: id});
			
			if (ROLESCONFIGWIN == null) {
				var store = new TreeStore({
					fields: ['id', 'aleaf', 'text', 'icon', 'checked'],
			        root: {
			        	id: 0,
			            expanded: true
			        },
			        proxy: {
			            type: 'ajax',
			            url: '../admin/role.do?configtomenu',
			            reader: {
			                type: 'json',
			                root: 'children'
			            }
			        },
			        listeners: {
			        	load: function() {
			        		roles.initAction(res);
			        	}
			        }
			    });
				ROLESCONFIGWIN = new Window({
					id: 'roleConfigWin',
				    title: '权限设置',
				    height: 600,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'treepanel',
				    	store: store,
				        hideHeaders: true,
				        rootVisible: false,
				        autoScroll: true,
				        width: '28%',
				        lines: false,
				        tbar: [{
				        	text: '刷新',
							scale: 'medium',
							iconCls: 'icon-refresh',
							handler: function() {
								store.load();
							}
				        }, "-", {
				        	text: '全部展开',
				        	scale: 'medium',
				        	iconCls: 'icon-open',
				        	handler: function() {
				        		this.up('treepanel').collapseAll();
				        		this.up('treepanel').expandAll();
				        	}
				        }],
				        listeners:{
				            "checkchange": function(node, state) {
				                if (node.get('leaf')) {
				                	if (state) {
				                		node.bubble(function(node){
					                        node.set('checked', state);  
					                        return true;   
					                    });
				                	}
				                } else {
				                	if (state) {
				                		node.bubble(function(node){
					                        node.set('checked', state);  
					                        return true;   
					                    });
				                	} else {
				                		node.cascade(function(node){
					                        node.set('checked', state);  
					                        return true;   
					                    });
				                	}
				                }  
				            }
				        }
				    }],
				    buttons: [{
				    	text: '确定',
				    	handler: function() {
				    		roles.configSaveAction();
				    	}
				    }, {
				    	text: '取消',
				    	handler: function() {
				    		ROLESCONFIGWIN.hide();
				    	}
				    }]
				});
			} else {
				roles.initAction(res);
			}
			ROLESCONFIGWIN.show();
		},
		
		/*--------------------------------------事件定义--------------------------------------------*/
		//搜索角色触发事件
		searchRoleAction: function() {
			roles.searchWin();
		},
		
		//搜索确定事件
		searchSureAction: function(fom) {
			var store = Ext.getCmp('rolegrid').getStore();
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				name: fom.findField('name').value
			};
			store.load();
			ROLESSEARCHWIN.hide();
		},
		
		//添加角色触发事件
		addRoleAction: function() {
			roles.addOrEditWin();
		},
		
		//修改角色触发事件
		editRoleAction: function() {
			var sel = Ext.getCmp('rolegrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要修改的角色！');
				return false;
			} else {
				roles.addOrEditWin();
				Ext.getCmp('roleAddOrEditForm').loadRecord(sel[0]);
			}
		},
		
		//提交确认事件
		sumbitAction: function(fom) {
			var url = '../admin/role.do?edit';
			var id = fom.findField('id').value;
			if (id == '') {
				url = '../admin/role.do?add';
			}
			FormSubmit(fom, url, {}, function(obj) {
				Ext.getCmp('rolegrid').getStore().load();
				ROLESADDOREDITWIN.hide();
			});
		},
		
		//删除角色事件
		removeAction: function() {
			var sel = Ext.getCmp('rolegrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要删除的角色！');
			} else {
				Ext.MessageBox.confirm('确认', '您确认要删除该角色吗？', function(btn) {
					if (btn == "yes") {
						var res = RequestByUrl('../admin/role.do?remove', {
							id: sel[0].get('id')
						});
						Ext.getCmp('rolegrid').getStore().load();
						Ext.Show.msg(res.title, '');
					}
				});
			}
		},
		
		//权限设置触发事件
		configAction: function() {
			var sel = Ext.getCmp('rolegrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请要选择要权限设置的角色！');
				return false;
			} else {
				roles.configWin(sel[0].get('id'));
			}
		},
		
		//权限保存事件
		configSaveAction: function() {
			var sel = Ext.getCmp('rolegrid').getSelectionModel().getSelection();
			var sms = Ext.getCmp('roleConfigWin').down('treepanel').getChecked();
			var rules = [];
			for (var i = 0; i < sms.length; i++) {
				if (sms[i].get('aleaf') > 1) {
					rules.push(sms[i].get('id'));
				}
			}
			var res = RequestByUrl('../admin/role.do?config', {
				id: sel[0].get('id'),
				rules: rules
			});
			Ext.Show.msg(res.title, '');
			ROLESCONFIGWIN.hide();
		},
		
		//初始化角色权限
		initAction: function(res) {
			var tree = Ext.getCmp('roleConfigWin').down('treepanel');
			var sms = tree.getChecked();
			var store = tree.getStore();
			for(var i = 0; i < sms.length; i++) {
				sms[i].set('checked', false);
			} 
			for (var i = 0; i < res.length; i++) {
				var cnode = store.getNodeById(parseInt(res[i]));
				cnode.set('checked', true);
				cnode.bubble(function(node){
	                node.set('checked', true);  
	                return true;   
	            });
			}
		}
	};
}();